﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using System.Web.UI;

namespace ASPNETVN.PORTAL.Administration.Module
{
    public partial class Permission : AddEditItemControl
    {

        protected override void OnInit(EventArgs e)
        {
            
            this.ButtonUpdate = this.btnUpdate;
            this.ButtonCancel = this.btnCancel;
            this.Update += new EventHandler(Permission_Update);
            base.OnInit(e);
        }

        void Permission_Update(object sender, EventArgs e)
        {
            object value = GetModule();
            if (value != null)
            {
                Components.Configuration.Items.Module m = (Components.Configuration.Items.Module)value;

                // View Roles.
                string viewRoles = "";
                foreach (ListItem item in chkViewRoles.Items)
                {
                    if (item.Selected)
                    {
                        viewRoles = viewRoles + item.Value + ";";
                    }
                }

                // Add Roles.
                string addRoles = "";
                foreach (ListItem item in chkAddRoles.Items)
                {
                    if (item.Selected)
                    {
                        addRoles = addRoles + item.Value + ";";
                    }
                }


                // Edit Roles.
                string editRoles = "";

                foreach (ListItem item in chkEditRoles.Items)
                {
                    if (item.Selected)
                    {
                        editRoles = editRoles + item.Value + ";";
                    }
                }

                // Delete Roles.
                string deleteRoles = "";
                foreach (ListItem item in chkDeleteRoles.Items)
                {
                    if (item.Selected)
                    {
                        deleteRoles = deleteRoles + item.Value + ";";
                    }
                }

                // Approve Roles.
                string approveRoles = "";
                foreach (ListItem item in chkApproveRole.Items)
                {
                    if (item.Selected)
                    {
                        approveRoles = approveRoles + item.Value + ";";
                    }
                }

                // Property Roles.
                string propertyRoles = "";
                foreach (ListItem item in chkPropertyRoles.Items)
                {
                    if (item.Selected)
                    {
                        propertyRoles = propertyRoles + item.Value + ";";
                    }
                }

                // Manager Roles.
                string managerRoles = "";
                foreach (ListItem item in chkManagerRoles.Items)
                {
                    if (item.Selected)
                    {
                        managerRoles = managerRoles + item.Value + ";";
                    }
                }

                // Security Roles.
                string securityRoles = "";
                foreach (ListItem item in chkSecurityRoles.Items)
                {
                    if (item.Selected)
                    {
                        securityRoles = securityRoles + item.Value + ";";
                    }
                }

                // Update module.
                PortalConfiguration config = new PortalConfiguration();
                config.UpdateModule(m.ID, viewRoles, addRoles, editRoles, deleteRoles, approveRoles, propertyRoles, managerRoles, securityRoles);
            }

            // Update custom permissions.
           
            for (int i = 0; i < rptCustomFunctions.Items.Count; i++)
            {
                Label lblFunctionID = rptCustomFunctions.Items[i].FindControl("lblFunctionID") as Label;
                CheckBoxList chkRoles = rptCustomFunctions.Items[i].FindControl("chkRoles") as CheckBoxList;
                ArrayList selectedRoles = new ArrayList();
                foreach (ListItem item in chkRoles.Items)
                {
                    if (item.Selected)
                    {
                        selectedRoles.Add(item.Value);
                    }
                }

                string[] roles = selectedRoles.ToArray(typeof(string)) as string[];
                PortalSecurity.UpdatePermissions(lblFunctionID.Text, this.ModuleID, roles);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        private Components.Configuration.Items.Module GetModule()
        {
            PortalDataSet ds = PortalConfiguration.GetPortalDataSet();
            PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(this.ModuleID);
            return Components.Configuration.Items.Module.LoadModuleRow(moduleRow);
        }

        private static bool CheckRole(string roleID, string roleString)
        {
            foreach (string r in roleString.Split(';'))
            {
                if (roleID.Trim() == r.Trim())
                {
                    return true;
                }
            }
            return false;
        }

        private void BindData()
        {
            // Obtain SiteSettings from Current Context
            PortalDataSet ds = PortalConfiguration.GetPortalDataSet();
            PortalDataSet.ModuleRow mRow = ds.Module.FindByID(this.ModuleID);
            Guid moduleDefinitionID = mRow.ModuleDefinitionID;

            // Custom permission.
            rptCustomFunctions.DataSource = Function.SelectBy_ModuleDefinitionID(moduleDefinitionID);
            rptCustomFunctions.DataBind();

            // All Permissions by ModuleID.
            DataTable dtPermissions = Components.Security.Permission.SelectBy_ModuleID(this.ModuleID).Tables[0];
            
            // All Roles by PortalID.
            DataTable dtRoles = Role.SelectAllByPortalID(Global.PortalID).Tables[0];
            for (int i = 0; i < rptCustomFunctions.Items.Count; i++)
            {
                Label lblFunctionID = rptCustomFunctions.Items[i].FindControl("lblFunctionID") as Label;
                CheckBoxList chkRoles = rptCustomFunctions.Items[i].FindControl("chkRoles") as CheckBoxList;
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = row["Name"].ToString();
                    item.Value = row["ID"].ToString();
                    if (dtPermissions.Select(string.Format("RoleID = {0} AND FunctionID = '{1}' AND ModuleID = '{2}'", item.Value, lblFunctionID.Text, this.ModuleID)).Length > 0)
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }
                    chkRoles.Items.Add(item);
                }
            }


            object value = GetModule();
            if (value != null)
            {
                Components.Configuration.Items.Module m = (Components.Configuration.Items.Module)value;
                

                // View roles.
                chkViewRoles.Items.Clear();
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = row["Name"].ToString();
                    item.Value = row["ID"].ToString();
                    if (CheckRole(item.Value, m.AuthorizedViewRoles))
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }
                    chkViewRoles.Items.Add(item);
                }


                // Add roles.
                chkAddRoles.Items.Clear();
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();
                    if (CheckRole(item.Value, m.AuthorizedAddRoles))
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }
                    chkAddRoles.Items.Add(item);
                }


                // Edit roles.
                chkEditRoles.Items.Clear();
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (CheckRole(item.Value, m.AuthorizedEditRoles))
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }

                    chkEditRoles.Items.Add(item);
                }

                // Delete roles.
                chkDeleteRoles.Items.Clear();
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (CheckRole(item.Value, m.AuthorizedDeleteRoles))
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }
                    chkDeleteRoles.Items.Add(item);
                }

                // Approve roles.
                chkApproveRole.Items.Clear();
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (CheckRole(item.Value, m.AuthorizedApproveRoles))
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }
                    chkApproveRole.Items.Add(item);
                }

                // Setting roles.
                chkPropertyRoles.Items.Clear();
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (CheckRole(item.Value, m.AuthorizedConfigureRoles))
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }
                    chkPropertyRoles.Items.Add(item);
                }


                // Manager roles.
                chkManagerRoles.Items.Clear();
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (CheckRole(item.Value, m.AuthorizedManageRoles))
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }
                    chkManagerRoles.Items.Add(item);
                }

                // Security roles.
                chkSecurityRoles.Items.Clear();
                foreach (DataRow row in dtRoles.Rows)
                {
                    ListItem item = new ListItem();
                    item.Text = (string)row["Name"];
                    item.Value = row["ID"].ToString();

                    if (CheckRole(item.Value, m.AuthorizedSecureRoles))
                    {
                        item.Selected = true;
                    }
                    // Administrators Role.
                    if (item.Value == "1")
                    {
                        item.Selected = true;
                        item.Enabled = false;
                    }
                    chkSecurityRoles.Items.Add(item);
                }
            }
        }
    }
}
